#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

template<class K,class V>
struct AVLTreeNode
{
	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right; 
	AVLTreeNode<K, V>* _parent;
	pair<K, V> _kv;//key和val

	int _bf;//balance factor平衡因子
			//右子树高度减左子树高度

	//构造函数 
	AVLTreeNode(const pair<K, V>& kv)
		:_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
		,_kv(kv)
		,_bf(0)
	{}
};

template<class K, class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
public:
	bool Insert(const pair<K,V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _root;

		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}//找到空位置了
		//开始插入
		cur = new Node(kv);
		if (parent->_kv.first < kv.first)
		{
			parent->_right = cur;
			cur->_parent = parent;
		}
		else
		{
			parent->_left = cur;
			cur->_parent = parent;
		}

		//AVL更新
		while (parent)//更新到根,即parent=nullptr退出
		{
			if (cur == parent->_left)
			{	//插入左边
				parent->_bf--;
			}
			else
			{	//插入右边
				parent->_bf++;
			}

			//此时：parent的平衡因子可能有三种情况：0，正负1， 正负2
			//1. 如果pParent的平衡因子为0，说明插入之前pParent的平衡因子
			//为正负1，插入后被调整成0，此时满足AVL树的性质，插入成功
			if (parent->_bf == 0)
			{
				break;
			}
			//2. 如果parent的平衡因子为正负1，说明插入前parent的平衡因子一定为0，
			//插入后被更新成正负1，此时以parent为根的树的高度增加，需要继续向上更新
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				cur = parent;
				parent = parent->_parent;
			}
			//3. 如果parent的平衡因子为正负2，则parent的平衡因子违反平衡树的性质，
			//需要对其进行旋转处理
			else if (parent->_bf == 2 || parent->_bf == -2)
			{//旋转
			//具体情况不用管，盯准bf平衡因子就行
				if (parent->_bf == 2 && cur->_bf == 1)
				{//左单旋	单纯的右边高
					RotateL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == -1)
				{//右单旋	单纯的左边高
					RotateR(parent);
				}
				else if (parent->_bf == 2 && cur->_bf == -1)
				{//右左双旋	右边高，但插入结点在右结点的左子树
					RotateRL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == 1)
				{//左右双旋	左边高，但插入结点在左结点的右子树
					RotateLR(parent);
				}
				else
				{//正常走不到这
					assert(false);
				}

				//1.旋转让这颗子树平衡了
				//2.选装降低了这颗子树的高度，恢复到根插入前一样的高度
				//	所以对上一层无影响，不用继续更新
				break;//旋转后，降低高度，插入成功
			}
			else
			{//正常走不到这
				assert(false);
			}
		}

		return true;
	}

	//左单旋
	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;//先改左右子树
		subR->_left = parent;

		Node* parentParent = parent->_parent;//记录父亲的父亲

		parent->_parent = subR;//更新parent
		if (subRL)//subRL可能为空
		{
			subRL->_parent = parent;
		}
		//更新subR的parent
		if (_root == parent)//根节点
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{//不是根节点
			if (parentParent->_left == parent)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}
			subR->_parent = parentParent;
		}

		parent->_bf = subR->_bf = 0;//平衡因子置零
	}

	//右单旋
	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		subL->_right = parent;

		Node* parentParent = parent->_parent;

		parent->_parent = subL;
		if (subLR)
		{
			subLR->_parent = parent;
		}
		if (_root == parent)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (parentParent->_left == parent)
			{
				parentParent->_left = subL;
			}
			else
			{
				parentParent->_right = subL;
			}
			subL->_parent = parentParent;
		}

		parent->_bf = subL->_bf = 0;
	}

	//右左双旋
	void RotateRL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		int bf = subRL->_bf;

		RotateR(parent->_right);
		RotateL(parent);

		if (bf == 0)
		{//subRL自己就是新增结点
			parent->_bf = subR->_bf = subRL->_bf = 0;
		}
		else if (bf == -1)
		{//在subRL左子树新增
			parent->_bf = 0;
			subRL->_bf = 0;
			subR->_bf = 1;
		}
		else if (bf == 1)
		{//在subRL右子树新增
			parent->_bf = -1;
			subRL->_bf = 0;
			subR->_bf = 0;
		}
		else
		{//正常走不到这
			assert(false);
		}
	}

	//左右双旋
	void RotateLR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		int bf = subLR->_bf;

		RotateL(parent->_left);
		RotateR(parent);

		if (bf == 0)
		{//subRL自己就是新增结点
			parent->_bf = subL->_bf = subLR->_bf = 0;
		}
		else if (bf == -1)
		{//在subRL左子树新增
			subL->_bf = 0;
			subLR->_bf = 0;
			parent->_bf = 1;
			
		}
		else if (bf == 1)
		{//在subRL右子树新增
			subL->_bf = -1;
			subLR->_bf = 0;
			parent->_bf = 0;
		}
		else
		{//正常走不到这
			assert(false);
		}
	}
	
	//查找O(logN)
	//保证查询时高效的时间复杂度，即$log_2 (N)$。
	//但是如果要对AVL树做一些结构修改的操作，性能非常低下
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
	}
	//中序遍历
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	
	//是否平衡
	bool IsBalance()
	{
		return _IsBalance(_root);
	}

	//高度
	int Height()
	{
		return _Height(_root);
	}
private:
	//中序(套上一层调用_root)
	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_kv.first << ":"<< root->_kv.second<<endl;
		_InOrder(root->_right);
	}

	//高度
	int _Height(Node* root)
	{
		if (root == nullptr)
		{
			return 0;
		}
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);

		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}
	//是否平衡
	bool _IsBalance(Node* root)
	{
		if (root == nullptr)
		{
			return true;
		}
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		if (rightHeight - leftHeight != root->_bf)
		{
			cout << root->_kv.first << "平衡因子异常" << endl;
			return false;
		}

		return abs(rightHeight - leftHeight) < 2//检查当前树
			&& _IsBalance(root->_left)	//检查左子树
			&& _IsBalance(root->_right);//检查右子树
	}
private:
	Node* _root = nullptr;

};